#pragma once 


#include <stdint.h>
#include <arpa/inet.h>
#include "server/rtmp/rtmp_protocol/rtmp_define.hpp"
#include "server/rtmp/rtmp_message/rtmp_message.hpp"

namespace mms {

class RtmpSetPeerBandWidthMessage{
public:
    #define LIMIT_TYPE_HARD 0
    #define LIMIT_TYPE_SOFT 1
    #define LIMIT_TYPE_DYNAMIC 2

public:
    RtmpSetPeerBandWidthMessage(uint32_t size, uint8_t limit_type):
        ack_window_size_(size), limit_type_(limit_type){}
    
    uint32_t size() {
        return 0;
    }


    int32_t decode(std::shared_ptr<RtmpMessage> rtmp_msg){
        uint8_t* payload = rtmp_msg->payload_;
        int32_t len = rtmp_msg->payload_size_;
        if(len < 5){
            return -1;
        }
        ack_window_size_ = ntohl(*(uint32_t*)payload);
        limit_type_ = payload[4];
        return 5;
    }


    std::shared_ptr<RtmpMessage> encode() {
        std::shared_ptr<RtmpMessage> rtmp_msg = std::make_shared<RtmpMessage>(
            sizeof(ack_window_size_) + sizeof(limit_type_));
        rtmp_msg->chunk_stream_id_ = RTMP_CHUNK_ID_PROTOCOL_CONTROL_MESSAGE;
        rtmp_msg->timestamp_ = 0;
        rtmp_msg->message_strean_id_ = RTMP_MESSAGE_ID_PROTOCOL_CONTROL;
        rtmp_msg->message_type_id_ = RTMP_CHUNK_MESSAGE_TYPE_SET_PEER_BANDWIDTH;

        uint8_t* payload = rtmp_msg->payload_;
        *(uint32_t*)payload = htonl(ack_window_size_);
        payload[4] = limit_type_;
        rtmp_msg->payload_size_ = sizeof(ack_window_size_) + sizeof(limit_type_);
        return rtmp_msg;
    }


private:
    uint32_t ack_window_size_ = 0;
    uint8_t limit_type_;

};


}